<template>
  <div class="content">
    <el-dialog :title="dlgCfg.title" :visible.sync="dlgCfg.isOpen" @close="onCloseDlg" :width="dlgCfg.width"
      :close-on-click-modal="false" destroy-on-close>
      <v-form-render ref="SelfFormRender" :form-json="dataCfg.formContent" :form-data="dataCfg.formData"
        :option-data="dataCfg.optionData" :global-dsv="dataCfgDsv" @download="onDownloadClick"></v-form-render>
      <div slot="footer">
        <!-- footer -->
        <!-- 表单申请 -->
        <div class="footer" v-if="!isView && !isPreview">
            <el-button v-if="curTaskOperateBtnConfig.curSubmitBtnEnable" type="primary" @click="onSubmitForm" icon="el-icon-circle-check">提交</el-button>
            <el-button v-if="curTaskOperateBtnConfig.curResetBtnEnable" @click="onResetForm" icon="el-icon-refresh">重置</el-button>
            <el-button v-if="curTaskOperateBtnConfig.curStagingBtnEnable" type="primary" @click="onSaveForm" icon="el-icon-collection">暂存</el-button>
            <el-button v-if="curTaskOperateBtnConfig.curCancelBtnEnable" type="danger" @click="onCancelForm" icon="el-icon-circle-close">撤销</el-button>
        </div>
        <!-- 表单设计详情预览 -->
        <div class="footer" v-else-if="isPreview">
            <el-button type="primary" @click="onSubmitForm" icon="el-icon-circle-check">提交</el-button>
            <el-button @click="onResetForm" icon="el-icon-refresh">重置</el-button>
        </div>
      </div>
    </el-dialog>
  </div>
</template>
  
<script>
import { isString } from '@/utils/validate'
import selfForm from "@/mixins/selfForm"
import selfFormRender from "@/mixins/selfFormRender"

export default {
  name: 'CptSelfFormDlg',
  mixins: [selfForm, selfFormRender],
  props: {
    formDataCfg: {
      type: Object,
      required: true,
      default: {}
    },
    isView: {
      type: Boolean,
      required: false,
      default: false
    },
    isDisable: {
      type: Boolean,
      required: false,
      default: false
    },
    isPreview: {
      type: Boolean,
      required: false,
      default: false
    },
    isFreezed: {
      type: Boolean,
      required: false,
      default: false
    },
  },
  data() {
    let dataCfg = {
      'formContent': {},
      'formData': {},
      'optionData': {}
    }
    if (this.formDataCfg) {
      if (this.formDataCfg.formContent) {
        dataCfg.formContent = isString(this.formDataCfg.formContent) ? JSON.parse(this.formDataCfg.formContent) : this.formDataCfg.formContent
      }
      dataCfg.formData = this.formDataCfg.formData ? this.formDataCfg.formData : {}
      dataCfg.optionData = this.formDataCfg.optionData ? this.formDataCfg.optionData : {}
    }
    console.log('CptSelfForm init dataCfg: ', dataCfg)
    return {
      dlgCfg: {
        isOpen: false,
        title: '表单配置详情',
        width: '80%',
        btnLoading: false
      },
      dataCfg,
      formConfCopy: {}
    }
  },
  mounted() {
    if (this.$refs['SelfFormRender']) {
      if (this.isDisable) {
        // this.$refs['SelfFormRender'].disableForm()
        this.$refs['SelfFormRender'].setReadMode(true)
      } else {
        if (this.$refs['SelfFormRender']._curSelfFormIsStraightEnableAllWidgets && this.$refs['SelfFormRender']._curSelfFormIsStraightEnableAllWidgets() === false) {
          console.warn('不可以直接启用组件表单（会影响默认禁用的组件）')
        } else {
          this.$refs['SelfFormRender'].enableForm()
        }
      }
    }
    this.initMyFormData()
  },
  methods: {
    showDlg(dataCfg) {
      if (dataCfg) {
        this.dataCfg = {...dataCfg}
        this.initMyFormData()
        console.log('showDlg dataCfg:', this.dataCfg)
        if (this.$refs['SelfFormRender']) {
          this.$refs['SelfFormRender'].setFormJson(this.dataCfg.formContent)
          this.$refs['SelfFormRender'].setFormData(this.dataCfg.formData) 
        }
      }
      this.dlgCfg.isOpen = true
    },
    onDlgOK() {
      this.$emit('submit')
    },
    onDlgCancel() {
      this.$emit('cancel')
    },
    onCloseDlg() {
      this.dataCfg = {
        'formContent': {},
        'formData': {},
        'optionData': {}
      }
      this.$emit('close')
    },
    initMyFormData() {
      // if (!this.isFreezed) {
      //   this.dataCfg.formData = {...this.dataCfg.formData, ...this.getFormApplyInfo()}
      // }
      this._initInnerFormData(this.dataCfg.formData)
    }
  }
}
</script>
  
<style lang='scss' scoped>
.content {

}
</style>